package antempire.control.game.operation.fishoperation;

import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.DelayModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.RotationByModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.modifier.IModifier;

import android.util.Log;
import antempire.control.game.GameConstants;
import antempire.control.game.operation.ModelInformationController;
import antempire.control.game.operation.wateroperation.WaterSprite;

public class AntController extends AnimatedSprite implements GameConstants{
	
	public PhysicsHandler mPhysicsHandler;	
	private AntOperation antOperation;
	AnimatedSprite capturedFish = null;
	
	int X,Y,A,way,up;
	
	int count_of_circle;
	Ant_Name name;
	Ant_Move move;
	@Override
	public void setRotation(float pRotation) {
		// TODO Auto-generated method stub
		// super.setRotation(pRotation);
		if(pRotation%360 >= -45 && pRotation%360 <= 45) {
			super.animate(new long[]{100, 100, 100}, 9, 11, true);
		}
		if(pRotation%360 > 45 && pRotation%360 <= 135) {
			super.animate(new long[]{100, 100, 100}, 0, 2, true);
		}
		if(pRotation%360 > 135 && pRotation%360 <= 225) {
			super.animate(new long[]{100, 100, 100}, 3, 5, true);
		}
		if(pRotation%360 > 225 && pRotation%360 <= 315) {
			super.animate(new long[]{100, 100, 100}, 6, 8, true);
		}
		if(pRotation%360 > 315 && pRotation%360 <= 405) {
			super.animate(new long[]{100, 100, 100}, 9, 11, true);
		}
		super.setRotation(360);
	}

	@Override
	public AnimatedSprite animate(long pFrameDurationEach) {
		// TODO Auto-generated method stub
		return super.animate(new long[]{pFrameDurationEach, pFrameDurationEach, pFrameDurationEach}, 3, 5, true);
	}

	float current_X,current_Y;
	float current_rotation;
	Move_Direction direction;
	Edge_Position position;
	
	public AntController(Ant_Name _name,TiledTextureRegion pTiledTextureRegion) {
		super(0, 0, pTiledTextureRegion);
		mPhysicsHandler=new PhysicsHandler(this);
		this.registerUpdateHandler(mPhysicsHandler);
		name=_name;
	}
	
	public void set_Ant_Operation(AntOperation operation)
	{
		antOperation=operation;
	}
	
	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		antOperation.fishOnManagedUpdate();
	}
	
	
	public Ant_Name getAntName(){
		return this.name;
	}

	public boolean isOutOfScene() {
		if(antOperation.isOutOfBound())
			return true;
		return false;
	}

	public void captured(TiledTextureRegion capturedFishTextureRegion,final BaseGameActivity activity, final int index){
//		this.detachSelf();
		SequenceEntityModifier entityModifier = new SequenceEntityModifier(
				new IEntityModifierListener() {

					@Override
					public void onModifierFinished(
							IModifier<IEntity> pModifier,
							final IEntity pItem) {
						capturedFish.animate(new long[]{10, 10, 10}, index, (index+2), 2, new IAnimationListener(){
							@Override
							public void onAnimationEnd(AnimatedSprite pAnimatedSprite){
								 activity.runOnUpdateThread(new Runnable() {
				                     @Override
				                     public void run() {
				                    	 activity.getEngine().getScene().getChild(LAYER_ANT).detachChild(capturedFish);
									}
								});
							}
						});
					}
				}, new SequenceEntityModifier(
						new ParallelEntityModifier(
								new ScaleModifier(0.8f, 1, 0.9f),
								new RotationModifier(0.8f, 0, 90),
								new AlphaModifier(0.8f, 1, 0.1f))));
		capturedFish =
			new AnimatedSprite(this.getX(), this.getY(),capturedFishTextureRegion.clone());
//		capturedFish.setSize(ResourcesOperation.getInstance().getFishInformation(name).getCaptureFishInformation().get_size_w(),
//				ResourcesOperation.getInstance().getFishInformation(name).getCaptureFishInformation().get_size_h());
		capturedFish.setSize(ModelInformationController.getInstance().getAntInformation(name).getCaptureAntInformation().get_size_w(),
				ModelInformationController.getInstance().getAntInformation(name).getCaptureAntInformation().get_size_h());
		capturedFish.animate(new long[]{50, 50, 50}, index, (index+2), 2);
		capturedFish.registerEntityModifier(entityModifier);
		activity.getEngine().getScene().getChild(LAYER_ANT).attachChild(capturedFish);
		
		
	}

	public boolean canBeCaughtByWater(WaterSprite waterSprite) {
		
		if(name == Ant_Name.MONOMORIUM)
			return CapturedProbability.getInstance().canBeCapturedByWater();
		else if(name == Ant_Name.PHEIDOLE)
			return CapturedProbability.getInstance().canBeCapturedByWater();
		else if(name == Ant_Name.LOS)
			return CapturedProbability.getInstance().canBeCapturedByWater();
		else if(name == Ant_Name.DOLICHODERINAE)
			return CapturedProbability.getInstance().canBeCapturedByWater();

		return false;
	}
	
	public boolean canBeCaughtByWater(AnimatedSprite captureWater, WaterSprite waterSprite) {
		
		if(name == Ant_Name.MONOMORIUM)
			return CapturedProbability.getInstance().canBeCapturedByWater();
		else if(name == Ant_Name.PHEIDOLE)
			return CapturedProbability.getInstance().canBeCapturedByWater();
		else if(name == Ant_Name.LOS)
			return CapturedProbability.getInstance().canBeCapturedByWater();
		else if(name == Ant_Name.DOLICHODERINAE)
			return CapturedProbability.getInstance().canBeCapturedByWater();
		return false;
	}
	
	
}
	

